package com.smallA.service.Impl;


import com.smallA.mapper.GradeMapper;
import com.smallA.mapper.QuestionMapper;
import com.smallA.mapper.TestMapper;
import com.smallA.mapper.TopicTypeMapper;
import com.smallA.po.*;
import com.smallA.service.TestService;
import com.smallA.utils.DataUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author 君未洋
 */
@Service("TestService")
public class TestServiceImpl implements TestService {

    @Resource
    private TestMapper testMapper;
    @Resource
    private TopicTypeMapper topicTypeMapper;
    @Resource
    private DataUtils dataUtils;
    @Resource
    private GradeMapper gradeMapper;
    @Resource
    private QuestionMapper questionMapper;

    @Override
    public List<Test> selectAllTest() {
        return testMapper.selectAllTest();
    }

    @Override
    public Test selectTestById(Integer id) {
        return testMapper.selectTestById(id);
    }

    @Override
    public boolean windingTest(Map map, Integer gid) {
        if (dataUtils.examineNumber(gid)) {
            return false;
        }
        Answer answer = new Answer();
        answer.setGid(gid);
        /*这里的思路挺简单的,就是通过题目的编号来获取答案*/
        /*查询数据库表去获取题目*/
        List<Question> questions = questionMapper.selectQuestionByGid(gid);
        for (Question question : questions) {
            Object data = map.remove(question.getId() + "");
            if (data != null) {
                answer.setAnswer(data.toString());
                answer.setQid(question.getId());
                testMapper.insertAnswer(answer);
            }
        }
        return true;
    }

    @Override
    public List<TopicType> selectAllTopicTpe() {
        return topicTypeMapper.selectAllTopicType();
    }

    @Override
    public boolean insertTest(Date beginDate, Integer examId, Integer useTime, String context) {
        if (dataUtils.examineNumber(examId, useTime)) {
            return false;
        }
        Test test = new Test(0, examId, context, beginDate, useTime);
        return testMapper.insertTest(test) > 0;
    }

    @Override
    public Integer selectTestByGradeId(Integer gid) {
        if (dataUtils.examineNumber(gid)) {
            return null;
        }
        return testMapper.selectTestByGradeId(gid);
    }

    @Override
    public boolean markGrade(Float[] fills, Float[] shorts, Exam exam, Integer gid) {
        if (dataUtils.examineNumber(gid)) {
            return false;
        }
        if (dataUtils.examineFloatArray(fills) && dataUtils.examineFloatArray(shorts)) {
            return false;
        }
        float score = 0;
        List<Question> questions = exam.getQuestions();
        for (int i = 0; i < questions.size(); i++) {
            if (questions.get(i).getType().equals("选择题")) {
                //选择题的情况下
                if (questions.get(i).getStudentAnswer().equals(questions.get(i).getAnswer())) {
                    score += questions.get(i).getScore();
                }
            } else {
                score += questions.get(i).getScore();
            }
        }
        gradeMapper.updateGradeScore(gid,score);
        return true;
    }


}
